package com.sx.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.cache.redis.Cache;
import com.dfec.cache.redis.Redis;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.PropertiesUtil;
import com.dfec.utils.ResponseUtil;
import com.dfec.utils.StringUtils;
import com.dfec.utils.Token;
import com.sx.service.UserLoginService;

/**
 * Servlet implementation class ForcastController
 */
@WebServlet(name = "us", description = "用户登录注册修改信息", urlPatterns = { "/us" })
public class UserLoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserLoginService s=new UserLoginService();
	private List<Object> list=null;
    Msg msg=null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		msg=new Msg();
		list=new ArrayList<>();
		List<Object> getdata = null ;
		String type = request.getParameter("type");
		try {
			if (type.equals("login")) {
				  String phone= request.getParameter("phone");
				  String pass= request.getParameter("pass");
				  String token = Token.makeToken(64);
				  s.updateToken(phone, token);
				  getdata = s.getUser(phone,pass); 
				 
				  if(!getdata.get(0).toString().equals("[]")){
						msg.setCode(Consists.DATA_Code_OK);
						msg.setMsg(Consists.DATA_Msg_OK);
						if(PropertiesUtil.config_redis_use) {
							//设置缓存token
							Cache ca = Redis.use();
							ca.setex(token,1000*60, phone);	
						}
						
					}else {
						msg.setCode(Consists.DATA_Code_OK__NODATA);
						msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
					}
				list.add(msg);
				list.add(getdata.get(0));
			}else if(type.equals("rigesiter")) {
				String phone= request.getParameter("phone");
				String city= request.getParameter("city");
				String cnty= request.getParameter("cnty");
				String town= request.getParameter("town");
				String fax_number= request.getParameter("fax_number");
				String name= request.getParameter("name");
				String work_unit= request.getParameter("work_unit");
				String post= request.getParameter("post");
				String other= request.getParameter("other");
				 String pass= request.getParameter("pass");
				List<Object> isUsing = s.getUser(phone);
				//账号被使用
				if(!isUsing.get(0).toString().equals("[]")) {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.Error_Msg__COUNT_ISUSING);
				}else {
					int i = s.rigesiter(phone,pass, city,cnty,town,fax_number,name,work_unit,post,other);
					if(i>0){
						msg.setCode(Consists.DATA_Code_OK);
						msg.setMsg(Consists.DATA_Msg_OK);
					}else {
						msg.setCode(Consists.DATA_Code_OK__NODATA);
						msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
					}
				}
				list.add(msg);
			}else if(type.equals("update")) {
				String phone= request.getParameter("phone");
				String city= request.getParameter("city");
				String cnty= request.getParameter("cnty");
				String town= request.getParameter("town");
				String fax_number= request.getParameter("fax_number");
				String name= request.getParameter("name");
				String work_unit= request.getParameter("work_unit");
				String post= request.getParameter("post");
				String other= request.getParameter("other");
				String ids=  request.getParameter("ids");
				int i = s.update(phone, city,cnty,town,fax_number,name,work_unit,post,other,ids);
				if(i>0){
					msg.setCode(Consists.DATA_Code_OK);
					msg.setMsg(Consists.DATA_Msg_OK);
				}else {
					msg.setCode(Consists.DATA_Code_OK__NODATA);
					msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
				}
				list.add(msg);
			}else if(type.equals("update_pass")) {
				String ids=  request.getParameter("ids");
				String pass=  request.getParameter("pass");
				String oldpass=  request.getParameter("oldpass");
				//没有找到对应账号
					int i = s.update(ids,pass,oldpass);
					if(i>0){
						msg.setCode(Consists.DATA_Code_OK);
						msg.setMsg(Consists.DATA_Msg_OK);
					}else {
						msg.setCode(Consists.DATA_Code_OK__NODATA);
						msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
					}
					list.add(msg);
			     }
				
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list.add(msg);
		}
	 ResponseUtil.sendScript(response, JSON.toJSONString(list));
	}

}
